package whf.framework.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

/**
 * @author wanghaifeng
 *
 */
public class Output {
	private OutputStream out = null;

	private Writer writer;

	public Output(OutputStream out) {
		this.out = out;
	}

	public Output(Writer writer) {
		this.writer = writer;
	}

	public Output print(Object obj) throws IOException {
		String s = obj == null ? "" : obj.toString();
		s += " ";
		if (out != null) {
			out.write(s.getBytes());
		} else if (writer != null) {
			writer.write(s);
		}
		return this;
	}

	public Output println() throws IOException {
		return println("");
	}

	public Output println(Object obj) throws IOException {
		String s = obj == null ? "" : obj.toString();
		s += " ";
		if (out != null) {
			out.write((System.getProperty("line.separator") + s).getBytes());
		} else if (writer != null) {
			writer.write(System.getProperty("line.separator") + s);
		}
		return this;
	}
	
	public void close() throws IOException{
		if(out != null)
			out.close();
		else if(writer != null)
			writer.close();
	}
	
	public void flush() throws IOException{
		if(out != null){
			out.flush();
		} else if(writer != null){
			writer.flush();
		}
	}
}
